<?php
/*
Template Name: Target
*/
?>
<?php 
//MOD:Build from Blog.php, using Target page intro info from Proximity theme
//
?>
<?php
function mct_eco_nowtraining($val){
    if ($val == 'eco_sidebar') return 'yes';
    return $val;
}
//set up display variables
$max_feat = 0;
$max_post = 0;
$full = true;
$more = false;

$qtopic = '';
$qaiclass = '';
$msg = '';

//handle get requests for topic and ai_class, ai_class is nested in previous topic
if (isset($_GET['topic'])){
    $qtopic = $_GET['topic'];
    set_transient('mct_ai_lasttopic',$qtopic,60*60);
}
elseif (isset($_GET['ai_class'])){
    $qaiclass = $_GET['ai_class'];
}
else {
    set_transient('mct_ai_lasttopic','');
}
    
$temp = $wp_query;
$wp_query= null;

$q_args = array(
    'post_type' => 'target_ai',
    'orderby' => 'date',
    'order' => 'DESC',
    'posts_per_page' => 10,
    'paged' => $paged
);
//Add topic, class query args
$msg = "Training Posts";
if (!empty($qtopic)){
    $qterm = get_term_by('slug',$qtopic,'topic');
    $q_args['topic'] = $qtopic;
    $msg = "Topic &raquo; ".$qterm->name;
}
if (!empty($qaiclass)){
    $lasttopic=get_transient('mct_ai_lasttopic');
    if (!empty($lasttopic)){
        $qterm = get_term_by('slug',$lasttopic,'topic');
        $q_args['topic'] = $lasttopic;
        $q_args['ai_class'] = $qaiclass;
        $msg = "Topic &raquo; ".$qterm->name." &middot Relevance &raquo; ".$qaiclass;
    } else {
        $q_args['ai_class'] = $qaiclass;
        $msg = "Relevance &raquo; ".$qaiclass;
    }
}
$wp_query = new WP_Query($q_args);
// Set up Filter to communicate with Sidebar, buried in header
add_filter('mct_eco_intraining','mct_eco_nowtraining');
?>
<?php get_header(); ?>

<div id="content">
<?php if ( have_posts() ) { ?>
        <h2 class="pagetitle"><?php echo $msg; ?></h2>
	<ul id="strips" class="noul">
		<?php
			$CATS = array();
			while ( have_posts() ) : the_post();
			
			$cats = array();
			$all_cats = get_the_category();
			foreach($all_cats as $c) {
				$cats['cat-'.$c->term_id] = $c->name;
				$CATS[$c->term_id] = $c->name;
			}
                        
		?>
                
		<li class="entry <?php echo implode(' ', array_keys($cats)); ?>">
				<?php image_attachment('mct_sl_image', '190', '240'); ?>
				<h1><?php the_title(); ?></h1>
				<div class="text"><?php prox_content_limit(350,''); //MOD: add special excerpt with link from prox ?></div>  
			<div class="meta">
				<span class="date"><?php the_time(get_option('date_format')); ?></span>
                                <?php do_action('prox_meta_home',$post->ID); //MOD: add training tools ?>
				<?php echo(get_the_term_target_ai($post->ID,'topic',' &middot ',',',' '));
                                      echo(get_the_term_target_ai($post->ID,'ai_class',' &middot ',',',' ')); 
                                ?> 
			</div>			

			<?php if(ide_option('comment_snippets')) ide_comment_snippets( $post->ID); ?>

			<div class="clear"> </div>
		</li>
		<?php
			endwhile;
		?>
	</ul>

	<div class="pagination">
		<div class="alignleft"><?php next_posts_link('&laquo; '._('Older Entries')); ?></div>
		<div class="alignright"><?php previous_posts_link(_('Newer Entries').' &raquo;'); ?></div>
		<div class="clear"> </div>
	</div>

	<script>
		var CATS = <?php echo json_encode($CATS); ?>;
	</script>
		
<?php } else {
		_e( !empty($empty_message) ? $empty_message : '<h1>Sorry!</h1>Sorry, the requested content was not found.');
	}
?>
</div><!-- content //-->
<?php remove_filter('mct_eco_intraining','mct_eco_nowtraining'); //remove the filter, done with it?> 
<?php get_footer(); ?>